package com.gxa.saas.sso.controller;

import com.gxa.saas.core.common.JsonResult;
import com.gxa.saas.sso.service.SmsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.Pattern;
import java.util.Map;

@RestController
@RequestMapping("/sms")
@Api("发送短信")
@Validated
public class SmsController {

    @Resource
    private SmsService smsService = null;

    @ApiOperation("发送短信")
    @PostMapping("/sendSms")
    public Map sendSms(
            @RequestParam("phone")
            @Pattern(regexp = "^[1](([3][0-9])|([4][5,7,9])|([5][0-9])|([6][6])|([7][3,5,6,7,8])|([8][0-9])|([9][8,9]))[0-9]{8}$",message = "手机格式不正确")
                               String phone) throws Exception {
        smsService.sendLoginSms(phone);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("短信验证码已经发送");
        return jsonResult.getMap();
    }
}
